home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / TEXFONT / simpletxf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  3.8 KB  |  184 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees  and is
  5.    provided without guarantee or warrantee expressed or  implied. This
  6.    program is -not- in the public domain. */
  7.  
  8. /* X compile line: cc -o simpletxf simpletxf.c texfont.c -lglut -lGLU -lGL -lXmu -lXext -lX11 -lm */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <GL/glut.h>
  14.  
  15. #include "TexFont.h"
  16.  
  17. int doubleBuffer = 1;
  18. char *filename = "default.txf";
  19. TexFont *txf;
  20. GLfloat angle = 20;
  21.  
  22. void
  23. idle(void)
  24. {
  25.   angle += 4;
  26.   glutPostRedisplay();
  27. }
  28.  
  29. void 
  30. visible(int vis)
  31. {
  32.   if (vis == GLUT_VISIBLE)
  33.     glutIdleFunc(idle);
  34.   else
  35.     glutIdleFunc(NULL);
  36. }
  37.  
  38. void
  39. display(void)
  40. {
  41.   char *str;
  42.  
  43.   /* Clear the color buffer. */
  44.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  45.  
  46.   glPushMatrix();
  47.  
  48.   glRotatef(angle, 0, 0, 1);
  49.   glTranslatef(-2.0, 0.0, 0.0);
  50.   glScalef(1 / 60.0, 1 / 60.0, 1 / 60.0);
  51.  
  52.   glPushMatrix();
  53.   glColor3f(0.0, 0.0, 1.0);
  54.   str = "OpenGL is";
  55.   txfRenderString(txf, str, (int) strlen(str));
  56.   glPopMatrix();
  57.  
  58.   glPushMatrix();
  59.   glColor3f(1.0, 0.0, 0.0);
  60.   glTranslatef(0.0, -60.0, 0.0);
  61.   str = "the best.";
  62.   txfRenderString(txf, str, (int) strlen(str));
  63.   glPopMatrix();
  64.  
  65.   glPopMatrix();
  66.  
  67.   /* Swap the buffers if necessary. */
  68.   if (doubleBuffer) {
  69.     glutSwapBuffers();
  70.   }
  71. }
  72.  
  73. int minifyMenu;
  74.  
  75. void
  76. minifySelect(int value)
  77. {
  78.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, value);
  79.   glutPostRedisplay();
  80. }
  81.  
  82. int alphaMenu;
  83.  
  84. void
  85. alphaSelect(int value)
  86. {
  87.   switch (value) {
  88.   case GL_ALPHA_TEST:
  89.     glDisable(GL_BLEND);
  90.     glEnable(GL_ALPHA_TEST);
  91.     glAlphaFunc(GL_GEQUAL, 0.5);
  92.     break;
  93.   case GL_BLEND:
  94.     glDisable(GL_ALPHA_TEST);
  95.     glEnable(GL_BLEND);
  96.     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  97.     break;
  98.   case GL_NONE:
  99.     glDisable(GL_ALPHA_TEST);
  100.     glDisable(GL_BLEND);
  101.     break;
  102.   }
  103. }
  104.  
  105. void
  106. mainSelect(int value)
  107. {
  108.   if (value == 666) {
  109.     exit(0);
  110.   }
  111. }
  112.  
  113. int
  114. main(int argc, char **argv)
  115. {
  116.   int i;
  117.  
  118.   glutInit(&argc, argv);
  119.   for (i = 1; i < argc; i++) {
  120.     if (!strcmp(argv[i], "-sb")) {
  121.       doubleBuffer = 0;
  122.     } else {
  123.       filename = argv[i];
  124.     }
  125.   }
  126.   if (filename == NULL) {
  127.     fprintf(stderr, "usage: show [GLUT-options] [-sb] txf-file\n");
  128.     exit(1);
  129.   }
  130.   txf = txfLoadFont(filename);
  131.   if (txf == NULL) {
  132.     fprintf(stderr, "Problem loading %s\n", filename);
  133.     exit(1);
  134.   }
  135.  
  136.   if (doubleBuffer) {
  137.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  138.   } else {
  139.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
  140.   }
  141.   glutInitWindowSize(300, 300);
  142.   glutCreateWindow("texfont");
  143.   glutDisplayFunc(display);
  144.  
  145.   glMatrixMode(GL_PROJECTION);
  146.   glLoadIdentity();
  147.   gluPerspective(60.0, 1.0, 0.1, 20.0);
  148.   glMatrixMode(GL_MODELVIEW);
  149.   gluLookAt(0.0, 0.0, 5.0,
  150.     0.0, 0.0, 0.0,
  151.     0.0, 1.0, 0.0);
  152.  
  153.   glEnable(GL_TEXTURE_2D);
  154.   glEnable(GL_DEPTH_TEST);
  155.   alphaSelect(GL_ALPHA_TEST);
  156.   minifySelect(GL_NEAREST);
  157.  
  158.   txfEstablishTexture(txf, 0, GL_TRUE);
  159.  
  160.   glutVisibilityFunc(visible);
  161.  
  162.   minifyMenu = glutCreateMenu(minifySelect);
  163.   glutAddMenuEntry("Nearest", GL_NEAREST);
  164.   glutAddMenuEntry("Linear", GL_LINEAR);
  165.   glutAddMenuEntry("Nearest mipmap nearest", GL_NEAREST_MIPMAP_NEAREST);
  166.   glutAddMenuEntry("Linear mipmap nearest", GL_LINEAR_MIPMAP_NEAREST);
  167.   glutAddMenuEntry("Nearest mipmap linear", GL_NEAREST_MIPMAP_LINEAR);
  168.   glutAddMenuEntry("Linear mipmap linear", GL_LINEAR_MIPMAP_LINEAR);
  169.  
  170.   alphaMenu = glutCreateMenu(alphaSelect);
  171.   glutAddMenuEntry("Alpha testing", GL_ALPHA_TEST);
  172.   glutAddMenuEntry("Alpha blending", GL_BLEND);
  173.   glutAddMenuEntry("Nothing", GL_NONE);
  174.  
  175.   glutCreateMenu(mainSelect);
  176.   glutAddSubMenu("Filtering", minifyMenu);
  177.   glutAddSubMenu("Alpha", alphaMenu);
  178.   glutAddMenuEntry("Quit", 666);
  179.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  180.  
  181.   glutMainLoop();
  182.   return 0;             /* ANSI C requires main to return int. */
  183. }
  184.